﻿using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;

namespace KxApp.Chart
{
    public partial class DeviceServiceImpl : ObservableObject, IDeviceService
    {

        public DeviceServiceImpl()
        {
            IniData();
        }

        //[ObservableProperty]
        //private ObservableCollection<KxDevice>? devices = new();

        public ObservableCollection<KxDevice> Devices { get; set; } = new();
        public ProductData ProductData { get; set; } = new();
        //private KxDevice? camera;
        //private Variable? cameraX;
        //private Variable? cameraY;



        #region Kr500
        public KxDevice Kr500 { get; set; } = new();

        public Variable Kr500v1 { get; set; } = new();

        public Variable Kr500v2 { get; set; } = new();
        public Variable Kr500v3 { get; set; } = new();
        public Variable Kr500v4 { get; set; } = new();
        public Variable Kr500v5 { get; set; } = new();
        public Variable Kr500v6 { get; set; } = new();
        public Variable Kr500v7 { get; set; } = new();

        public Variable? Kr500Pgno { get; set; } = new();
        public Variable? Kr500PdctId { get; set; } = new();

        #endregion

        #region Kr16
        public KxDevice Kr16 { get; set; } = new();

        public Variable Kr16v1 { get; set; } = new();

        public Variable Kr16v2 { get; set; } = new();

        public Variable Kr16v3 { get; set; } = new();
        public Variable Kr16v4 { get; set; } = new();
        public Variable Kr16v5 { get; set; } = new();
        public Variable Kr16v6 { get; set; } = new();
        public Variable Kr16v7 { get; set; } = new();

        public Variable Kr16Pgno { get; set; } = new();
        public Variable Kr16PdctId { get; set; } = new();
        #endregion

        #region 相机
        public KxDevice Camera { get; set; } = new();
        public Variable CameraX { get; set; } = new();
        public Variable CameraY { get; set; } = new();
        #endregion

        #region 底座1伺服
        public KxDevice DZ1Servo { get; set; } = new();
        public Variable DZ1ActPos { get; set; } = new();
        public Variable DZ1SetPos { get; set; } = new();
        public Variable DZ1ActVel { get; set; } = new();

        #endregion

        #region 底座2伺服
        public KxDevice DZ2Servo { get; set; } = new();
        public Variable DZ2ActPos { get; set; } = new();
        public Variable DZ2SetPos { get; set; } = new();
        public Variable DZ2ActVel { get; set; } = new();

        #endregion

        #region 底座3伺服
        public KxDevice DZ3Servo { get; set; } = new();
        public Variable DZ3ActPos { get; set; } = new();
        public Variable DZ3SetPos { get; set; } = new();
        public Variable DZ3ActVel { get; set; } = new();

        #endregion

        #region 框架1伺服
        public KxDevice KJ1Servo { get; set; } = new();
        public Variable KJ1ActPos { get; set; } = new();
        public Variable KJ1SetPos { get; set; } = new();
        public Variable KJ1ActVel { get; set; } = new();

        #endregion

        #region 框架2伺服
        public KxDevice KJ2Servo { get; set; } = new();
        public Variable KJ2ActPos { get; set; } = new();
        public Variable KJ2SetPos { get; set; } = new();
        public Variable KJ2ActVel { get; set; } = new();

        #endregion

        #region 框架3伺服
        public KxDevice KJ3Servo { get; set; } = new();
        public Variable KJ3ActPos { get; set; } = new();
        public Variable KJ3SetPos { get; set; } = new();
        public Variable KJ3ActVel { get; set; } = new();

        #endregion

        #region 轴伺服
        public KxDevice AxisServo { get; set; } = new();
        public Variable AxisActPos { get; set; } = new();
        public Variable AxisSetPos { get; set; } = new();
        public Variable AxisActVel { get; set; } = new();

        #endregion

        #region 打磨伺服
        public KxDevice PolishServo { get; set; } = new();
        public Variable PolishActPos { get; set; } = new();
        public Variable PolishSetPos { get; set; } = new();
        public Variable PolishActVel { get; set; } = new();

        #endregion

        public void IniData()
        {
            InitialRobot();
            InitialCamera();
            InitialServo();
        }


        private void InitialRobot()
        {
            Kr500 = new()
            {
                Id = 1,
                DeviceName = $"搬运机器人",
                DeviceNum = 1,
                AlarmInfo = $"报警号",
                State = 1,
                Variables = new ObservableCollection<Variable>()
            };

            Kr500v1 = new() { VariableDescription = "1轴电流", CurrentValue = "", Units = "A" };
            Kr500v2 = new() { VariableDescription = "2轴电流", CurrentValue = "", Units = "A" };
            Kr500v3 = new() { VariableDescription = "3轴电流", CurrentValue = "", Units = "A" };
            Kr500v4 = new() { VariableDescription = "4轴电流", CurrentValue = "", Units = "A" };
            Kr500v5 = new() { VariableDescription = "5轴电流", CurrentValue = "", Units = "A" };
            Kr500v6 = new() { VariableDescription = "6轴电流", CurrentValue = "", Units = "A" };
            Kr500v7 = new() { VariableDescription = "7轴电流", CurrentValue = "", Units = "A" };

            Kr500Pgno = new() { VariableDescription = "Rob程序号", CurrentValue = "", };

            Kr500PdctId = new() { VariableDescription = "当前产品号", CurrentValue = "", };

            //Kr500.Variables.Add(Kr500v1);
            //Kr500.Variables.Add(Kr500v2);
            Kr500.Variables.Add(Kr500Pgno);
            Kr500.Variables.Add(Kr500PdctId);
            Kr500.Variables.Add(Kr500v1);
            Kr500.Variables.Add(Kr500v2);
            Kr500.Variables.Add(Kr500v3);
            Kr500.Variables.Add(Kr500v4);
            Kr500.Variables.Add(Kr500v5);
            Kr500.Variables.Add(Kr500v6);
            Kr500.Variables.Add(Kr500v7);
            /////////
            Kr16 = new()
            {
                Id = 2,
                DeviceName = $"立式加工中心1",
                DeviceNum = 2,
                AlarmInfo = $"报警号",
                State = 3,
                Variables = new ObservableCollection<Variable>()
            };

            Kr16v1 = new() { VariableDescription = "1轴电流", CurrentValue = "", Units = "A" };
            Kr16v2 = new() { VariableDescription = "2轴电流", CurrentValue = "", Units = "A" };
            Kr16v3 = new() { VariableDescription = "3轴电流", CurrentValue = "", Units = "A" };
            Kr16v4 = new() { VariableDescription = "4轴电流", CurrentValue = "", Units = "A" };
            Kr16v5 = new() { VariableDescription = "5轴电流", CurrentValue = "", Units = "A" };
            Kr16v6 = new() { VariableDescription = "6轴电流", CurrentValue = "", Units = "A" };
            Kr16v7 = new() { VariableDescription = "7轴电流", CurrentValue = "", Units = "A" };

            Kr16Pgno = new() { VariableDescription = "Rob程序号", CurrentValue = "", };

            Kr16PdctId = new() { VariableDescription = "当前产品号", CurrentValue = "", };

            //Kr16.Variables.Add(Kr16v1);
            //Kr16.Variables.Add(Kr16v2);
            Kr16.Variables.Add(Kr16Pgno);
            Kr16.Variables.Add(Kr16PdctId);
            Kr16.Variables.Add(Kr16v1);
            Kr16.Variables.Add(Kr16v2);
            Kr16.Variables.Add(Kr16v3);
            Kr16.Variables.Add(Kr16v4);
            Kr16.Variables.Add(Kr16v5);
            Kr16.Variables.Add(Kr16v6);
            Kr16.Variables.Add(Kr16v7);

            Devices.Add(Kr500);
            Devices.Add(Kr16);
        }


        private void InitialCamera()
        {
            Camera = new()
            {
                Id = 3,
                DeviceName = $"Hikvision相机",
                DeviceNum = 3,
                AlarmInfo = $"报警号",
                State = 1,
                Variables = new ObservableCollection<Variable>()
            };
            CameraX = new() { VariableDescription = "相机X坐标", CurrentValue = "", };
            CameraY = new() { VariableDescription = "相机Y坐标", CurrentValue = "", };
            Camera.Variables.Add(CameraX);
            Camera.Variables.Add(CameraY);

            Devices.Add(Camera);

        }

        private void InitialServo()
        {
            DZ1Servo = new()
            {
                Id = 4,
                DeviceName = $"卧式加工中心1",
                DeviceNum = 4,
                AlarmInfo = $"报警号",
                State = 2,
                Variables = new ObservableCollection<Variable>()
            };

            DZ1ActPos = new() { VariableDescription = "实际位置", CurrentValue = "", Units = "mm" };
            DZ1ActVel = new() { VariableDescription = "实际速度", CurrentValue = "", Units = "mm/s" };
            DZ1SetPos = new() { VariableDescription = "设定位置", CurrentValue = "", Units = "mm" };

            DZ1Servo.Variables.Add(DZ1SetPos);
            DZ1Servo.Variables.Add(DZ1ActPos);
            DZ1Servo.Variables.Add(DZ1ActVel);

            Devices.Add(DZ1Servo);

            DZ2Servo = new()
            {
                Id = 5,
                DeviceName = $"卧式加工中心2",
                DeviceNum = 5,
                AlarmInfo = $"报警号",
                State = 2,
                Variables = new ObservableCollection<Variable>()
            };

            DZ2ActPos = new() { VariableDescription = "实际位置", CurrentValue = "", Units = "mm" };
            DZ2ActVel = new() { VariableDescription = "实际速度", CurrentValue = "", Units = "mm/s" };
            DZ2SetPos = new() { VariableDescription = "设定位置", CurrentValue = "", Units = "mm" };

            DZ2Servo.Variables.Add(DZ2SetPos);
            DZ2Servo.Variables.Add(DZ2ActPos);
            DZ2Servo.Variables.Add(DZ2ActVel);

            Devices.Add(DZ2Servo);


            DZ3Servo = new()
            {
                Id = 6,
                DeviceName = $"卧式加工中心3",
                DeviceNum = 6,
                AlarmInfo = $"报警号",
                State = 2,
                Variables = new ObservableCollection<Variable>()
            };

            DZ3ActPos = new() { VariableDescription = "实际位置", CurrentValue = "", Units = "mm" };
            DZ3ActVel = new() { VariableDescription = "实际速度", CurrentValue = "", Units = "mm/s" };
            DZ3SetPos = new() { VariableDescription = "设定位置", CurrentValue = "", Units = "mm" };

            DZ3Servo.Variables.Add(DZ3SetPos);
            DZ3Servo.Variables.Add(DZ3ActPos);
            DZ3Servo.Variables.Add(DZ3ActVel);

            Devices.Add(DZ3Servo);


            KJ1Servo = new()
            {
                Id = 7,
                DeviceName = $"立式加工中心2",
                DeviceNum = 7,
                AlarmInfo = $"报警号",
                State = 2,
                Variables = new ObservableCollection<Variable>()
            };

            KJ1ActPos = new() { VariableDescription = "实际位置", CurrentValue = "", Units = "mm" };
            KJ1ActVel = new() { VariableDescription = "实际速度", CurrentValue = "", Units = "mm/s" };
            KJ1SetPos = new() { VariableDescription = "设定位置", CurrentValue = "", Units = "mm" };

            KJ1Servo.Variables.Add(KJ1SetPos);
            KJ1Servo.Variables.Add(KJ1ActPos);
            KJ1Servo.Variables.Add(KJ1ActVel);

            Devices.Add(KJ1Servo);


            KJ2Servo = new()
            {
                Id = 8,
                DeviceName = $"立式加工中心3",
                DeviceNum = 8,
                AlarmInfo = $"报警号",
                State = 2,
                Variables = new ObservableCollection<Variable>()
            };

            KJ2ActPos = new() { VariableDescription = "实际位置", CurrentValue = "", Units = "mm" };
            KJ2ActVel = new() { VariableDescription = "实际速度", CurrentValue = "", Units = "mm/s" };
            KJ2SetPos = new() { VariableDescription = "设定位置", CurrentValue = "", Units = "mm" };

            KJ2Servo.Variables.Add(KJ2SetPos);
            KJ2Servo.Variables.Add(KJ2ActPos);
            KJ2Servo.Variables.Add(KJ2ActVel);

            Devices.Add(KJ2Servo);

            KJ3Servo = new()
            {
                Id = 9,
                DeviceName = $"立式加工中心4",
                DeviceNum = 9,
                AlarmInfo = $"报警号",
                State = 2,
                Variables = new ObservableCollection<Variable>()
            };

            KJ3ActPos = new() { VariableDescription = "实际位置", CurrentValue = "", Units = "mm" };
            KJ3ActVel = new() { VariableDescription = "实际速度", CurrentValue = "", Units = "mm/s" };
            KJ3SetPos = new() { VariableDescription = "设定位置", CurrentValue = "", Units = "mm" };

            KJ3Servo.Variables.Add(KJ3SetPos);
            KJ3Servo.Variables.Add(KJ3ActPos);
            KJ3Servo.Variables.Add(KJ3ActVel);

            Devices.Add(KJ3Servo);


            AxisServo = new()
            {
                Id = 10,
                DeviceName = $"立式加工中心5",
                DeviceNum = 10,
                AlarmInfo = $"报警号",
                State = 2,
                Variables = new ObservableCollection<Variable>()
            };

            AxisActPos = new() { VariableDescription = "实际位置", CurrentValue = "", Units = "mm" };
            AxisActVel = new() { VariableDescription = "实际速度", CurrentValue = "", Units = "mm/s" };
            AxisSetPos = new() { VariableDescription = "设定位置", CurrentValue = "", Units = "mm" };

            AxisServo.Variables.Add(AxisSetPos);
            AxisServo.Variables.Add(AxisActPos);
            AxisServo.Variables.Add(AxisActVel);

            Devices.Add(AxisServo);


            PolishServo = new()
            {
                Id = 11,
                DeviceName = $"珩磨机",
                DeviceNum = 11,
                AlarmInfo = $"报警号",
                State = 2,
                Variables = new ObservableCollection<Variable>()
            };

            PolishActPos = new() { VariableDescription = "实际位置", CurrentValue = "", Units = "mm" };
            PolishActVel = new() { VariableDescription = "实际速度", CurrentValue = "", Units = "mm/s" };
            PolishSetPos = new() { VariableDescription = "设定位置", CurrentValue = "", Units = "mm" };

            PolishServo.Variables.Add(PolishSetPos);
            PolishServo.Variables.Add(PolishActPos);
            PolishServo.Variables.Add(PolishActVel);

            Devices.Add(PolishServo);
        }
    }
}
